package com.ruoyi.file.controller;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.file.domain.TblFile;
import com.ruoyi.file.domain.TifInfo;
import com.ruoyi.file.service.ITblFileService;
import com.ruoyi.gis.domain.DatasetRasterTaskNode;
import com.ruoyi.gis.domain.DatasetTypeBand;
import com.ruoyi.gis.earthai.vo.GetTaskNodeBandVo;
import com.ruoyi.gis.flow.DatasetItem;
import com.ruoyi.gis.flow.StartNodeParam;
import com.ruoyi.gis.service.IDatasetTypeBandService;
import com.ruoyi.gis.vo.FlowNodeBandVo;
import com.ruoyi.gis.vo.TaskFlowConfig;
import jodd.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/flow")
public class TaskFileBandsController {

    @Autowired
    private ITblFileService tblFileService;
    @Autowired
    private IDatasetTypeBandService datasetTypeBandService;

    @PostMapping("/task/node/bands")
    public AjaxResult taskNodeBands(
            @RequestBody GetTaskNodeBandVo getTaskNodeBandVo, HttpServletResponse response)
    {
        TaskFlowConfig flow = getTaskNodeBandVo.getFlow();
        if(flow==null||flow.getNodeList()==null||flow.getLineList()==null){
            return AjaxResult.error("流程错误");
        }
        DatasetRasterTaskNode startNode = flow.getNodeList().stream().
                filter(i->"start".equalsIgnoreCase(i.getType())).findFirst().orElseGet(null);
        if(startNode==null|| StringUtil.isEmpty(startNode.getConfig())){
            return AjaxResult.error("请配置流程开始节点");
        }
        List<StartNodeParam> startNodeParams = JSON.parseArray(startNode.getConfig(), StartNodeParam.class);
        if(startNodeParams==null|| startNodeParams.size()==0){
            return AjaxResult.error("请配置流程开始节点");
        }
        StartNodeParam startNodeParam = startNodeParams.get(0);
        if(startNodeParam==null|| startNodeParam.getParams()==null
                ||startNodeParam.getParams().getDatasets()==null){
            return AjaxResult.error("请配置流程开始节点");
        }
        List<DatasetItem> datasets = startNodeParam.getParams().getDatasets();
        List<String> sourceTypes = datasets.stream().map(DatasetItem::getSourceType).collect(Collectors.toList());
        if(sourceTypes==null|| sourceTypes.size()>1){
            return AjaxResult.error("节点输入数据类型只能是同一种类型");
        }
        DatasetItem datasetItem = datasets.get(0);
        if(datasetItem.getFileId()==null){
            return AjaxResult.error("请配置流程开始节点");
        }
        TblFile tblFile = tblFileService.selectTblFileById(datasetItem.getFileId());
        if(tblFile==null){
            return AjaxResult.error("数据配置错误");
        }
        List<FlowNodeBandVo> bandVos = new ArrayList<>();
        if(tblFile.getDataId()==null){
            if(tblFile.getFileInfo()==null){
                TifInfo tifInfo = JSON.parseObject(tblFile.getFileInfo(),TifInfo.class);
                if(tifInfo!=null&&tifInfo.getBands()!=null){
                    for(int i=0;i<tifInfo.getBands().size();i++){
                        TifInfo.TifBand tifBand = tifInfo.getBands().get(i);
                        FlowNodeBandVo band = new FlowNodeBandVo();
                        band.setSort(i+1);
                        band.setBandNo((i+1)+"");
                        band.setName(tifBand.getDescription());
                        band.setSourceType("raster");
                        bandVos.add(band);
                    }
                }
            }
        }else {
            DatasetTypeBand query = new DatasetTypeBand();
            query.setSourceType(tblFile.getSourceType());
            List<DatasetTypeBand> datasetTypeBands = datasetTypeBandService.
                    selectDatasetTypeBandList(query);
            for(int i=0;i<datasetTypeBands.size();i++){
                DatasetTypeBand tifBand = datasetTypeBands.get(i);
                FlowNodeBandVo band = new FlowNodeBandVo();
                band.setSort(i+1);
                band.setBandNo(tifBand.getBandNo());
                band.setName(tifBand.getBandName());
                band.setSourceType(tifBand.getSourceType());
                bandVos.add(band);
            }
        }
        return AjaxResult.success("操作成功",bandVos);
    }
}
